//
//  ZDXAlertView.m
//  HeSheQu
//
//  Created by xue on 2018/1/26.
//  Copyright © 2018年 APPLE. All rights reserved.
//

#import "ZDXAlertView.h"
///alertView  宽
#define AlertW SCREEN_WIDTH-80
@interface ZDXAlertView()
// 背景视图
@property (nonatomic, strong) UIView *backgroundView;
//弹窗
@property (nonatomic,retain) UIImageView *alertView;
//内容
@property (nonatomic,retain) UILabel *msgLbl;


@end
@implementation ZDXAlertView

- (instancetype)initWithMessage:(NSString *)message image:(NSString*)image
{
    if (self == [super init]) {
        self.frame = [UIScreen mainScreen].bounds;
        [self addSubview:self.backgroundView];
        
        self.alertView = [[UIImageView alloc] init];
        self.alertView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        self.alertView.layer.cornerRadius = 10.0;
        self.alertView.clipsToBounds =YES;
        
        self.alertView.frame = CGRectMake(0, 0, AlertW, 240);
        self.alertView.layer.position = self.center;
//        self.alertView.image =[UIImage imageNamed:@"integral_bg"];
        
        
        UIImageView *duigou =[[UIImageView alloc]initWithFrame:CGRectMake((AlertW-40)/2, 24, 40, 40)];
        duigou.image =[UIImage imageNamed:image];
        [self.alertView addSubview:duigou];
        
        
        self.msgLbl =[[UILabel alloc]initWithFrame:CGRectMake(35, CGRectGetMaxY(duigou.frame)+30, AlertW-2*35, 20)];
        self.msgLbl.text =message;
        self.msgLbl.font =[UIFont systemFontOfSize:14.0];
        self.msgLbl.textColor =[UIColor whiteColor];
        self.msgLbl.textAlignment = NSTextAlignmentCenter;
        self.msgLbl.numberOfLines=0;
        [self.alertView addSubview:self.msgLbl];
        CGSize titleSize = [self.msgLbl.text boundingRectWithSize:CGSizeMake(_msgLbl.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_msgLbl.font} context:nil].size;
        CGRect size =_msgLbl.frame;
        size.size.height =titleSize.height+2;
        _msgLbl.frame =size;
        
        
        
        //计算高度
        CGFloat alertHeight = CGRectGetMaxY(self.msgLbl.frame)+30;
        self.alertView.frame = CGRectMake(0, 0, AlertW, alertHeight);
        self.alertView.layer.position = self.center;
        
        [self addSubview:self.alertView];

    }
    return self;
}

#pragma mark - 弹出 -
- (void)showXLAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
    [self performSelector:@selector(Click_hidden) withObject:nil afterDelay:1.5];
}
-(void)Click_hidden{
    
    [self removeFromSuperview];
}
#pragma mark - 背景遮罩图层
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _backgroundView.backgroundColor = [UIColor clearColor];
//        _backgroundView.userInteractionEnabled = YES;
//        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
//        [_backgroundView addGestureRecognizer:myTap];
    }
    return _backgroundView;
}

- (void)creatShowAnimation
{
    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
    }];
}



-(UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@end
